package com.liqw.activiti.service.impl;

import com.liqw.activiti.service.ProcessService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@AllArgsConstructor
@Service
public class ProcessServiceImpl implements ProcessService {
    private final RepositoryService repositoryService;

    private final RuntimeService runtimeService;

    private final HistoryService historyService;

    @Override
    public Deployment deploy(String fileName) {
        DeploymentBuilder builder = repositoryService.createDeployment();
        builder.addClasspathResource("processes/" + fileName);
        builder.key("simple");
        return builder.deploy();
    }

    @Override
    public ProcessInstance start(String processDefinitionKey) {
        ProcessInstance test01 = runtimeService.startProcessInstanceByKey(processDefinitionKey);
        String id = test01.getId();
        System.out.println("流程id=" + id);
        return test01;
    }

    @Override
    public boolean stop(String processId) {
        runtimeService.deleteProcessInstance(processId, "结束");
        return true;
    }

    @Override
    public List<HistoricActivityInstance> taskList() {
        return historyService.createHistoricActivityInstanceQuery().list();
    }
}
